package com.inews.business.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inews.base.mapper.BeanMapper;
import com.inews.base.persistence.DynamicSpecifications;
import com.inews.base.persistence.SearchFilter;
import com.inews.base.persistence.SearchFilter.Operator;
import com.inews.base.utils.ExtDataUtil;
import com.inews.base.utils.InewsUtils;
import com.inews.base.vo.ExtData;
import com.inews.business.constants.InewsBusinessConstants;
import com.inews.business.entity.TblRetireStaffHis;
import com.inews.business.repository.RetireStaffHisDao;
import com.inews.business.vo.RetireStaffHisVo;
import com.inews.business.vo.RetireStaffVo;

@Service
@Transactional(readOnly=true)
public class RetireStaffHisServiceImpl implements RetireStaffHisService, InewsBusinessConstants{

	@Autowired
	private RetireStaffHisDao retireStaffHisDao;
	@Autowired
	private RetireStaffService retireStaffService;
	
	/**
	 * 分页查询退休人员历史信息.
	 *
	 * @param filterParams the filter params
	 * @param page the page
	 * @return the ext data
	 */
	public ExtData<RetireStaffHisVo> findRetireStaffHisPage(Map<String, Object> filterParams, Pageable page, RetireStaffVo vo){
		List<RetireStaffHisVo> voList = new ArrayList<RetireStaffHisVo>();
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		filters.put("retireId", new SearchFilter("retireId", Operator.EQ, vo.getRetireId()));
		Page<TblRetireStaffHis> pagePoList = retireStaffHisDao.findAll(DynamicSpecifications.bySearchFilter(filters.values(), TblRetireStaffHis.class), page);
		for (TblRetireStaffHis po : pagePoList.getContent()) {
			RetireStaffHisVo hisVo = BeanMapper.map(po, RetireStaffHisVo.class);
			hisVo.setBatchDateFormat(InewsUtils.fomatDate(po.getBatchDate()));
			hisVo.setRetireDateFormat(InewsUtils.fomatDate(po.getRetireDate()));
			hisVo.setCountryPayFormat(InewsUtils.formatCurrency2String(po.getCountryPay()));
			hisVo.setCountryPayAddFormat(InewsUtils.formatCurrency2String(po.getCountryPayAdd()));
			hisVo.setAllowanceFormat(InewsUtils.formatCurrency2String(po.getAllowance()));
			Long basePay = po.getCountryPay() + po.getCountryPayAdd();
			hisVo.setBasePayFormat(InewsUtils.formatCurrency2String(basePay));
			Long sumPay = po.getCountryPay() + po.getCountryPayAdd() + po.getAllowance();
			hisVo.setSumPayFormat(InewsUtils.formatCurrency2String(sumPay));
			voList.add(hisVo);
		}
		ExtData<RetireStaffHisVo> extData = ExtDataUtil.genWithPagingList(voList, pagePoList.getTotalElements());
		return extData;
	}
	
	
	/**
	 * 导出excel.
	 *
	 * @param filterParams the filter params
	 * @param page the page
	 * @return the ext data
	 */
	public ExtData<RetireStaffHisVo> exportExcel(Map<String, Object> filterParams){
		List<RetireStaffHisVo> allHisVoList = new ArrayList<RetireStaffHisVo>();
		ExtData<RetireStaffVo> data = retireStaffService.findRetireStaffPage(filterParams, null);
		List<RetireStaffVo> voList = data.getRoot();
		for (RetireStaffVo vo : voList) {
			ExtData<RetireStaffHisVo> hisData = findRetireStaffHisPage(new HashMap<String, Object>(), null, vo);
			List<RetireStaffHisVo> hisVoList = hisData.getRoot();
			for (RetireStaffHisVo hisVo : hisVoList) {
				allHisVoList.add(hisVo);
			}
		}
		return ExtDataUtil.genWithData(allHisVoList);
	}
	
}
